#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
def colortext(col,str):
    if col == 'red':
        return '\033[1;31m%s\033[0m'%str
    elif col == 'pink':
        return '\033[1;35m%s\033[0m'%str
    elif col == 'gold':
        return '\033[1;33m%s\033[0m'%str
    elif col == 'blue':
        return '\033[1;34m%s\033[0m'%str
    else:
        return str
def get_the_file_line(filepath):
    fileid=file(filepath,'r')
    i=0
    while True:
        s=fileid.readline()
        if len(s) !=0:
            i=i+1
        else:
            break
    fileid.close()
    return i
def lastnamesame(s,sduibi,n):
    if s[len(s)-n:len(s)]==sduibi:
        return True
    else:
        return False
def get_the_last_(s):#abcd_aaaa 返回4
    n = 0
    tmp = s
    while tmp.find('_') != -1:
        tmp = tmp[tmp.find('_')+1:]
        n = s.find(tmp)-1
    return n
def noxg(s):
    while s[len(s)-1:len(s)]=='/':
        s=s[0:len(s)-1]
    return s
def check_root():
    pipe=os.popen('/usr/bin/id -u')
    a=pipe.read()
    pipe.close()
    a=a[0:len(a)-1]
    if a=='0':
        return True
    else:
        return False
def get_the_dir_path(dirpath):
    home = os.environ['HOME']
    if dirpath[0:2]=='./':#如果是以./开头的，表示是当前工作目录下的目录
        path=os.getcwd()+'/'+dirpath[2:len(dirpath)]
        return path
    elif dirpath[0:3]=='../':#如果是以../开头的，表示是工作目录上层目录下的目录
        path=os.path.dirname(os.getcwd())+dirpath[2:len(dirpath)]
        return path
    elif dirpath[0:1]=='/':#如果是以/开头的，表示第三个参数即为目录
        path=dirpath
        return path
    elif dirpath[0:1]=='~':
        path = dirpath.replace('~','%s'%home)
        return path
    else:
        path=os.getcwd()+'/'+dirpath
        return path
def get_the_dir_size(path):
    home = os.environ['HOME'] #/home/user
    tmp_file_list = '%s/.tmp/tmp_file_list'%(home)
    os.system('find %s >%s '%(path,tmp_file_list))
    f_line_num = get_the_file_line(tmp_file_list) #获取文件行数
    f = file(tmp_file_list,'r')
    i = 0
    sum = 0
    for i in range(0,f_line_num):
        line_str = f.readline()[:-1]
        if os.path.isfile(line_str):
            #print os.path.getsize(line_str)
            sum = sum + os.path.getsize(line_str)
            #print 'sum=%s'%sum
        elif os.path.isdir(line_str):
            #print os.path.getsize(line_str)
            sum = sum + 4096
    f.close()
    return sum
